package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/prometheus/client_golang/prometheus"
	"http_monitor/collector"
	"http_monitor/models"
)

// 基础控制器
type MetricController struct {
	beego.Controller
}

func (c *MetricController) Get() {
	result := ""

	if chn, err := collector.GetRequestTime(); err == nil {
		close(chn)
		for value := range chn {
			apis := value["models"].(models.Apis)
			result += fmt.Sprintf("osc_aegis_http_request_time{address=\"%s\",area=\"%s\",name=\"%s\"} %d", apis.Url, "test", apis.Name, value["requestTime"])
			prometheus.NewDesc(
				"api",
				"no",
				[]string{"url"},
				prometheus.Labels{"api_name": "url"},
			)
		}
	}
	c.Data["json"] = result
	c.ServeJSON()
}
